package com.basics.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basics.channel.entity.CyChannelRule;
import com.basics.channel.mapper.CyChannelRuleMapper;
import com.basics.channel.service.CyChannelRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basics.common.exception.CustomException;
import com.basics.common.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 帖子屏蔽规则 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-03-09
 */
@Service
public class CyChannelRuleServiceImpl extends ServiceImpl<CyChannelRuleMapper, CyChannelRule> implements CyChannelRuleService {

    @Override
    public CyChannelRule getByChannelId(Long channelId) {
        return getOne(new QueryWrapper<CyChannelRule>().eq("channel_id",channelId));
    }

    @Override
    public void checkContentRule(String content,String imageUrls,Long channelId){
        CyChannelRule rule = getByChannelId(channelId);
        if(rule==null){
            return;
        }
        if(StringUtils.isNotEmpty(content)){
            if(rule.getAllowUrl()!=null &&rule.getAllowUrl()){
                if(StringUtils.containsURL(content)){
                    throw new CustomException("帖子内容中包含网址");
                }
            }
            if(StringUtils.isNotEmpty(rule.getKeywords())){
                String[] split = rule.getKeywords().split(",");
                for (String s : split) {
                    if(content.contains(s)){
                        throw new CustomException("帖子内容中包含敏感词");
                    }
                }
            }
        }
        if(StringUtils.isNotEmpty(imageUrls)){
            //@TODO 处理图片过滤

        }
    }


}
